package com.zw.yzk.kabu.view.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.RadioGroup;

import com.zw.yzk.kabu.R;


/**
 * Created by wei on 2016/8/26.
 */
public class ChangeCosplayCategoryDialog {
    private PopupWindow popupWindow;
    private Context context;

    public ChangeCosplayCategoryDialog(Context context) {
        this.context = context;
    }

    //创建popupwindow
    private void createPopupWindow(Context context) {
        if (popupWindow == null) {
            View rootView = LayoutInflater.from(context).inflate(R.layout.dialog_select_cosplay_category, null);
            RadioGroup radioGroup = (RadioGroup) rootView;
            radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    switch (checkedId) {
                        case R.id.cos_all:
                            if (listener != null) {
                                listener.onCheck(0);
                            }
                            break;
                        case R.id.cos_recommend:
                            if (listener != null) {
                                listener.onCheck(1);
                            }
                            break;
                        case R.id.cos_concerned:
                            if (listener != null) {
                                listener.onCheck(2);
                            }
                            break;
                        case R.id.cos_celebrity_list:
                            if (listener != null) {
                                listener.onCheck(3);
                            }
                            break;
                        default:
                            break;
                    }
                    dismiss();
                }
            });

            //加载对话框布局
            popupWindow = new PopupWindow(rootView, context.getResources().getDimensionPixelSize(R.dimen.select_cosplay_category_dialog_width),
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            //设置点击popupwindow外消失
            popupWindow.setBackgroundDrawable(new ColorDrawable());
            popupWindow.setOutsideTouchable(true);
            popupWindow.setFocusable(true);
        }

    }


    //显示
    public void showAsDropDown(View view) {
        createPopupWindow(context);
        popupWindow.showAsDropDown(view, (view.getWidth() - popupWindow.getWidth()) / 2, 0);
    }

    //关闭popupwindow
    public void dismiss() {
        if (popupWindow == null) {
            return;
        }
        popupWindow.dismiss();
    }

    public interface OnItemCheckedListener {
        void onCheck(int position);
    }

    private OnItemCheckedListener listener;

    //设置item点击监听事件
    public void setOnItemChcekedListener(OnItemCheckedListener listener) {
        this.listener = listener;
    }
}
